﻿// This file is part of Code4Public (http://code4public.codeplex.com)
// Copyright 2011 Sina Iravanian - <sina@sinairv.com>
//
// This source file(s) may be redistributed, altered and customized
// by any means PROVIDING the authors name and all copyright
// notices remain intact.
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED. USE IT AT YOUR OWN RISK. THE AUTHOR ACCEPTS NO
// LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
// ------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Code4Public.GUI
{
    public partial class InputDialog : Form
    {
        public InputDialog()
        {
            InitializeComponent();
        }

        private Func<string, bool> m_inputAcceptanceRule;

        public static string ShowInputDialog(string message, string caption, string defaultInput)
        {
            return ShowInputDialog(message, caption, defaultInput, null);
        }

        public static string ShowInputDialog(string message, string caption, string defaultInput, Func<string, bool> inputAcceptanceRule)
        {
            using (InputDialog frm = new InputDialog())
            {
                frm.lblMessage.Text = message;
                frm.Text = caption;
                frm.m_inputAcceptanceRule = inputAcceptanceRule;
                frm.txtInput.Focus();

                if (defaultInput != null)
                {
                    frm.txtInput.Text = defaultInput;
                    frm.txtInput.SelectAll();
                }

                DialogResult dr = frm.ShowDialog();
                if (dr == DialogResult.Cancel)
                    return null;
                else
                {
                    return frm.txtInput.Text;
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.m_inputAcceptanceRule != null)
            {
                if (this.m_inputAcceptanceRule(this.txtInput.Text))
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
